from app.extension.database.sqlalchemy import sqla_db as db
from datetime import datetime
from .base_model import BaseModel


class AddressModel(db.Model, BaseModel):

    """用户地址模型"""

    __tablename__ = 'tb_address'
    id = db.Column(db.Integer, primary_key=True, comment='主键')
    mobile = db.Column(db.String(15), comment='电话号码')
    name = db.Column(db.String(80), comment='联系人姓名')
    create_time = db.Column(db.DateTime, default=datetime.now)
    delete_time = db.Column(db.DateTime)
    province = db.Column(db.String(80))
    city = db.Column(db.String(80))
    country = db.Column(db.String(80))
    detail = db.Column(db.String(200), comment='地址详情')
    user_id = db.Column(db.Integer, comment='关联的用户ID')

    default_hidden = ['delete_time', 'create_time', 'user_id']

    def __init__(self, data: dict, user_id):
        self.mobile = data.get('mobile')
        self.name = data.get('name')
        self.province = data.get('province')
        self.city = data.get('city')
        self.country = data.get('country')
        self.detail = data.get('detail')
        self.user_id = user_id

    def update(self, data: dict):
        self.mobile = data.get('mobile')
        self.name = data.get('name')
        self.province = data.get('province')
        self.city = data.get('city')
        self.country = data.get('country')
        self.detail = data.get('detail')